<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StorePack;
use App\Http\Requests\UpdatePack;
use App\Models\Item;
use App\Models\Pack;
use App\Models\PreferCate;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PackController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_hd' => 'am-in',
            '_pack' => 'am-active',
        ]);

    }
    //挑战项目管理首页
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按挑战名称
            if($request->has('name')and $request->name !=''){
                $search = "%".$request->name."%";
                $query->where('name','like',$search);
            }
            //按门店
            if($request->has('store_id')and $request->store_id !='-1'){

                $query->where('store_id',$request->store_id);
            }
        };
        //所有门店
        $cates=Store::all();
        //挑战关联门店
        $packs=Pack::where($where)->with('store')->orderBy('sort')->paginate(20);


        return view('admin.pack.index',compact('packs','cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //新增页面
    public function create()
    {
        //所有门店
        $cates=Store::all();
        return view('admin.pack.create',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(StorePack $request)
    {

        Pack::create($request->all());
        return redirect('/admin/pack')->with('success','新增套餐成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //编辑页面
    public function edit($id)
    {
        //所有门店
        $cates=Store::all();
        //当前挑战项目
        $pack=Pack::find($id);
        return view('admin.pack.edit',compact('cates','pack'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //执行更新
    public function update(UpdatePack $request, $id)
    {
        //当前挑战项目
       $pack=Pack::find($id);
       $date['name']=$request->name;
       $date['store_id']=$request->store_id;

       $info=Pack::where($date)->first();
       //保证挑战项目名称唯一性
       if($pack['name']!=$request->name && isset($info)){
           return back()->with('success','所修改套餐名字以有同一门店其他套餐使用');
       }
       $pack->update($request->all());
       return redirect('/admin/pack')->with('success','修改成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //待完善，需判断所产出套餐是否有用户正在参与
    public function destroy($id)
    {
        $info=Item::where('pack_id',$id)->first();
        if($info){
            return back()->with('success','当前套餐下有项目不能删除');
        }else{
            Pack::destroy($id);
            return back()->with('success','删除成功');
        }
    }
    //改变使用状态
    public function change_attr(Request $request)
    {

        $pack = Pack::find($request->id);

        if ($pack->is_use == 0) {
            $data['is_use'] = 1;
            $pack->update($data);
        }else {
            $data['is_use'] = 0;
            $pack->update($data);
        }
    }
     //Ajax排序
    public function sort(Request $request){
        $pack = Pack::find($request->id);
        $pack->sort = $request->sort;
        $pack->save();
    }
}
